package controllers.event;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import controllers.AbstractController;

import services.EventService;
import domain.Event;

@Controller
@RequestMapping("/event")
public class EventController extends AbstractController {
	@Autowired
	private EventService eventService;

	public EventController() {
		super();
	}
	//Listing
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Collection<Event> events;
		events = eventService.findEventsAvailable();
		
		result = new ModelAndView("event/list");
		result.addObject("requestURI", "event/list.do");
		result.addObject("events", events);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	@RequestMapping(value = "/ongoing/list", method = RequestMethod.GET)
	public ModelAndView ongoingList() {
		ModelAndView result;
		Collection<Event> events;
		events = eventService.findEventsOngoing();
		result = new ModelAndView("event/list");
		
		result.addObject("requestURI", "event/list.do");
		result.addObject("events", events);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	@RequestMapping(value = "/ordered/list", method = RequestMethod.GET)
	public ModelAndView orderedList() {
		ModelAndView result;

		Collection<Event> events = eventService.findAllOrderedByRating();

		result = new ModelAndView("event/list");
		result.addObject("requestURI", "event/list.do");
		result.addObject("events", events);
		result.addObject("ordered", true);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}
	//View
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int eventId) {
		ModelAndView result;
		Event event = eventService.findOne(eventId);
		Boolean isOwner = false;
		Boolean isNew = false;
		Boolean editable = false;
		
		result = new ModelAndView("event/edit");
		result.addObject("event", event);
		result.addObject("requestURI", "event/edit.do");
		result.addObject("isOwner", isOwner);
		result.addObject("isNew", isNew);
		result.addObject("editable", editable);
		result.addObject("backURI", "/event/list.do");
		
		
		return result;
	}
	
	// Ancillary methods

		protected ModelAndView createEditModelAndView(Event event) {
			ModelAndView result;
			result = createEditModelAndView(event, null);
			return result;

		}

		protected ModelAndView createEditModelAndView(Event event, String message) {
			ModelAndView result;

			result = new ModelAndView("event/edit");
			result.addObject("event", event);
			result.addObject("message", message);

			return result;

		}
}
